package com.dream.technology.controller;

import com.dream.technology.data.GenericRspDTO;
import com.dream.technology.db.entity.RcFlowLog;
import com.dream.technology.db.entity.RcFlowLogQuery;
import com.dream.technology.service.RcFlowLogService;
import com.dream.technology.vo.ErrorCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("标签规则命中")
@RestController
@RequestMapping("/risk/admin/flow_log")
public class RcFlowLogController {

    private static final Logger logger = LoggerFactory.getLogger(RcFlowLogController.class);

    @Autowired
    RcFlowLogService rcFlowLogService;

    /**
     * 标签规则命中
     * @param pageNum
     * @param pageSize
     * @param cusName
     * @param mobile
     * @return
     */
    @GetMapping("/list")
    public GenericRspDTO<Object> getList(Integer pageNum, Integer pageSize, String cusName, 
    		String mobile, String startDate, String endDate, String itemName){
        RcFlowLogQuery logQuery =new RcFlowLogQuery();
        RcFlowLogQuery.Criteria criteria = logQuery.createCriteria();
        if (StringUtils.isNotBlank(cusName)) {
        	criteria.andCusNameLike("%" + cusName + "%");
        }
        if (StringUtils.isNotBlank(itemName)) {
        	criteria.andItemNameLike("%" + itemName + "%");
        }
        if (StringUtils.isNotBlank(mobile)) {
        	criteria.andMobileEqualTo(mobile);
        }
        if (startDate != null) {
//        	criteria.andCreateDateBetween(DateUtils.pageSizearseDate(startDate), DateUtils.parseDate(endDate));
        }
        logQuery.setOrderByClause("create_date desc");
        PageHelper.startPage(pageNum,pageSize);
        List<RcFlowLog> rcFlowLogList = rcFlowLogService.selectByExample(logQuery);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), new PageInfo<RcFlowLog>(rcFlowLogList));
    }

    @GetMapping("/id")
    public GenericRspDTO<Object> getById(Long id){

        RcFlowLog rcFlowLog = rcFlowLogService.selectByPrimaryKey(id);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), rcFlowLog);
    }

    @ApiOperation("风险命中列表 根据用户id和风险编号,触发内容 查询,用户中心-用户详情")
    @PostMapping("/selectControlFlowLog")
    public GenericRspDTO<Object> selectControlFlowLog(Long customerId, String name,Integer pageNum,Integer pageSize){

        logger.info("风险命中列表查询 请求参数 customerId={},name={},pageNum={},pageSize={}",customerId,name,pageNum,pageSize);

        PageHelper.startPage(pageNum,pageSize);
        List<RcFlowLog> rcFlowLogList = rcFlowLogService.selectControlFlowLog(customerId,name);
        PageInfo pageInfo=new PageInfo(rcFlowLogList);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), pageInfo);
    }
}
